<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>cbc.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_crypt_cbc.js.html">cbc.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Implementation of CBC mode for block ciphers.  See
<a name="line17"></a> *     http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation
<a name="line18"></a> *     #Cipher-block_chaining_.28CBC.29. for description.
<a name="line19"></a> *
<a name="line20"></a> * @author nnaze@google.com (Nathan Naze)
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.crypt.Cbc&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.array&#39;);
<a name="line26"></a>goog.require(&#39;goog.crypt&#39;);
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>/**
<a name="line31"></a> * Implements the CBC mode for block ciphers. See
<a name="line32"></a> * http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation
<a name="line33"></a> * #Cipher-block_chaining_.28CBC.29
<a name="line34"></a> *
<a name="line35"></a> * @param {!goog.crypt.BlockCipher} cipher The block cipher to use.
<a name="line36"></a> * @param {number=} opt_blockSize The block size of the cipher in bytes.
<a name="line37"></a> *     Defaults to 16 bytes.
<a name="line38"></a> * @constructor
<a name="line39"></a> */
<a name="line40"></a>goog.crypt.Cbc = function(cipher, opt_blockSize) {
<a name="line41"></a>
<a name="line42"></a>  /**
<a name="line43"></a>   * Block cipher.
<a name="line44"></a>   * @type {!goog.crypt.BlockCipher}
<a name="line45"></a>   * @private
<a name="line46"></a>   */
<a name="line47"></a>  this.cipher_ = cipher;
<a name="line48"></a>
<a name="line49"></a>  /**
<a name="line50"></a>   * Block size in bytes.
<a name="line51"></a>   * @type {number}
<a name="line52"></a>   * @private
<a name="line53"></a>   */
<a name="line54"></a>  this.blockSize_ = opt_blockSize || 16;
<a name="line55"></a>};
<a name="line56"></a>
<a name="line57"></a>
<a name="line58"></a>/**
<a name="line59"></a> * Encrypt a message.
<a name="line60"></a> *
<a name="line61"></a> * @param {!Array.&lt;number&gt;} plainText Message to encrypt. An array of bytes.
<a name="line62"></a> *     The length should be a multiple of the block size.
<a name="line63"></a> * @param {!Array.&lt;number&gt;} initialVector Initial vector for the CBC mode.
<a name="line64"></a> *     An array of bytes with the same length as the block size.
<a name="line65"></a> * @return {!Array.&lt;number&gt;} Encrypted message.
<a name="line66"></a> */
<a name="line67"></a>goog.crypt.Cbc.prototype.encrypt = function(plainText, initialVector) {
<a name="line68"></a>
<a name="line69"></a>  goog.asserts.assert(
<a name="line70"></a>      plainText.length % this.blockSize_ == 0,
<a name="line71"></a>      &#39;Data\&#39;s length must be multiple of block size.&#39;);
<a name="line72"></a>
<a name="line73"></a>  goog.asserts.assert(
<a name="line74"></a>      initialVector.length == this.blockSize_,
<a name="line75"></a>      &#39;Initial vector must be size of one block.&#39;);
<a name="line76"></a>
<a name="line77"></a>  // Implementation of
<a name="line78"></a>  // http://en.wikipedia.org/wiki/File:Cbc_encryption.png
<a name="line79"></a>
<a name="line80"></a>  var cipherText = [];
<a name="line81"></a>  var vector = initialVector;
<a name="line82"></a>
<a name="line83"></a>  // Generate each block of the encrypted cypher text.
<a name="line84"></a>  for (var blockStartIndex = 0;
<a name="line85"></a>       blockStartIndex &lt; plainText.length;
<a name="line86"></a>       blockStartIndex += this.blockSize_) {
<a name="line87"></a>
<a name="line88"></a>    // Takes one block from the input message.
<a name="line89"></a>    var plainTextBlock = goog.array.slice(
<a name="line90"></a>        plainText,
<a name="line91"></a>        blockStartIndex,
<a name="line92"></a>        blockStartIndex + this.blockSize_);
<a name="line93"></a>
<a name="line94"></a>    var input = goog.crypt.xorByteArray(plainTextBlock, vector);
<a name="line95"></a>    var resultBlock = this.cipher_.encrypt(input);
<a name="line96"></a>
<a name="line97"></a>    goog.array.extend(cipherText, resultBlock);
<a name="line98"></a>    vector = resultBlock;
<a name="line99"></a>  }
<a name="line100"></a>
<a name="line101"></a>  return cipherText;
<a name="line102"></a>};
<a name="line103"></a>
<a name="line104"></a>
<a name="line105"></a>/**
<a name="line106"></a> * Decrypt a message.
<a name="line107"></a> *
<a name="line108"></a> * @param {!Array.&lt;number&gt;} cipherText Message to decrypt. An array of bytes.
<a name="line109"></a> *     The length should be a multiple of the block size.
<a name="line110"></a> * @param {!Array.&lt;number&gt;} initialVector Initial vector for the CBC mode.
<a name="line111"></a> *     An array of bytes with the same length as the block size.
<a name="line112"></a> * @return {!Array.&lt;number&gt;} Decrypted message.
<a name="line113"></a> */
<a name="line114"></a>goog.crypt.Cbc.prototype.decrypt = function(cipherText, initialVector) {
<a name="line115"></a>
<a name="line116"></a>  goog.asserts.assert(
<a name="line117"></a>      cipherText.length % this.blockSize_ == 0,
<a name="line118"></a>      &#39;Data\&#39;s length must be multiple of block size.&#39;);
<a name="line119"></a>
<a name="line120"></a>  goog.asserts.assert(
<a name="line121"></a>      initialVector.length == this.blockSize_,
<a name="line122"></a>      &#39;Initial vector must be size of one block.&#39;);
<a name="line123"></a>
<a name="line124"></a>  // Implementation of
<a name="line125"></a>  // http://en.wikipedia.org/wiki/File:Cbc_decryption.png
<a name="line126"></a>
<a name="line127"></a>  var plainText = [];
<a name="line128"></a>  var blockStartIndex = 0;
<a name="line129"></a>  var vector = initialVector;
<a name="line130"></a>
<a name="line131"></a>  // Generate each block of the decrypted plain text.
<a name="line132"></a>  while (blockStartIndex &lt; cipherText.length) {
<a name="line133"></a>
<a name="line134"></a>    // Takes one block.
<a name="line135"></a>    var cipherTextBlock = goog.array.slice(
<a name="line136"></a>        cipherText,
<a name="line137"></a>        blockStartIndex,
<a name="line138"></a>        blockStartIndex + this.blockSize_);
<a name="line139"></a>
<a name="line140"></a>    var resultBlock = this.cipher_.decrypt(cipherTextBlock);
<a name="line141"></a>    var plainTextBlock = goog.crypt.xorByteArray(vector, resultBlock);
<a name="line142"></a>
<a name="line143"></a>    goog.array.extend(plainText, plainTextBlock);
<a name="line144"></a>    vector = cipherTextBlock;
<a name="line145"></a>
<a name="line146"></a>    blockStartIndex += this.blockSize_;
<a name="line147"></a>  }
<a name="line148"></a>
<a name="line149"></a>  return plainText;
<a name="line150"></a>};
</pre>


</body>
</html>
